package com.cvmars.jianyou.module.fragment;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cvmars.jianyou.R;
import com.cvmars.jianyou.api.api.Api;
import com.cvmars.jianyou.api.api.HttpUtils;
import com.cvmars.jianyou.api.api.SimpleSubscriber;
import com.cvmars.jianyou.api.model.HttpResult;
import com.cvmars.jianyou.module.activity.FindDetailActivity;
import com.cvmars.jianyou.module.activity.FindTopicActivity;
import com.cvmars.jianyou.module.adapter.FindAdapter;
import com.cvmars.jianyou.module.adapter.FindTopicAdapter;
import com.cvmars.jianyou.module.base.BaseFragment;
import com.cvmars.jianyou.module.model.EventFindRefresh;
import com.cvmars.jianyou.module.model.FindListModel;
import com.cvmars.jianyou.module.model.FindModel;
import com.cvmars.jianyou.module.model.TopicListModel;
import com.cvmars.jianyou.module.model.TopicModel;
import com.cvmars.jianyou.ui.pulltorefresh.PulltoRefreshRecyclerView;
import com.cvmars.jianyou.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hehaifeng on 2018/11/26.
 */

public class TopicFragment extends BaseFragment {


    PulltoRefreshRecyclerView listHome;

    FindTopicAdapter homeAdapter;

    boolean isHot;

    List<TopicModel> modelList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.frg_home_index, null);

        listHome = view.findViewById(R.id.list_home);

        EventBus.getDefault().register(this);
        homeAdapter = new FindTopicAdapter(getActivity(), R.layout.item_find_topic, modelList);
        Bundle arguments = getArguments();


        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), FindTopicActivity.class);
                intent.putExtra("model", modelList.get(position));
//                getActivity().startActivity(intent);

                ActivityOptions options = null;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    options = ActivityOptions.makeSceneTransitionAnimation(getActivity(), view,
                            "iv");
                    startActivity(intent, options.toBundle());
                } else {
                    startActivity(intent);
                }
            }
        });


//        homeAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
//            @Override
//            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
//                onDeleteTOpic(modelList.get(position).id);
//
//                return false;
//            }
//        });

        listHome.setAdapter(homeAdapter);


        if (arguments != null) {

            isHot = arguments.getBoolean("isHot", false);
        }

        listHome.setPullRefreshListener(new PulltoRefreshRecyclerView.RecyPtrHandler() {
            @Override
            public void onLoadMore() {

                request();
            }

            @Override
            public void onRefresh() {


                request();


            }
        });


        request();
        listHome.loadMoreEnd();
        return view;
    }



    private void onDeleteTOpic(String id){


        HttpUtils.getInstance().toSubscribe(Api.getInstance().deleteFindTopic(id)
                , new SimpleSubscriber<HttpResult<Object>>() {
                    @Override
                    protected void _onNext(HttpResult<Object> o) {

                        ToastUtils.show("删除成功");

                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventRefresh(EventFindRefresh refresh) {

        if (refresh.isRefresh) {

            listHome.mCurPager = 1;
            request();
        }
    }

    private void request() {


        HttpUtils.getInstance().toSubscribe(Api.getInstance().getTopicList()
                , new SimpleSubscriber<HttpResult<TopicListModel>>() {
                    @Override
                    protected void _onNext(HttpResult<TopicListModel> o) {

                        getLoadDialogAndDismiss();

                        TopicListModel data = o.getData();

                        if (data != null) {

                            List<TopicModel> results = data.results;

                            if (results != null) {

                                if (listHome.mCurPager == 1) {
                                    modelList.clear();
                                }
                                modelList.addAll(results);
                                homeAdapter.notifyDataSetChanged();
                                listHome.refreshComplete();
                                listHome.loadMoreComplete();

                            }
                            listHome.loadMoreEnd();
                        }
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
